from rest_framework import serializers
from .models import *


class ChannelsSerializers(serializers.ModelSerializer):

    class Meta:
        model = Channel
        fields = '__all__'


class ArticleSimpleSerializers(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields = ['id', 'title']


class UserArticleSerializers(serializers.ModelSerializer):

    articles = ArticleSimpleSerializers(many=True, read_only=True)

    class Meta:
        model = User
        fields = ['id', 'username', 'avatar', 'articles', 'fans']


class SubCommentSerializers(serializers.ModelSerializer):

    user = UserArticleSerializers(many=True, read_only=True)
    subs = serializers.StringRelatedField()

    class Meta:
        model = Comment
        fields = '__all__'


class CommentsSerializers(serializers.ModelSerializer):

    user = UserArticleSerializers(read_only=True)
    subs = SubCommentSerializers(many=True, read_only=True)

    class Meta:
        model = Comment
        fields = '__all__'


class ArticleSerializers(serializers.ModelSerializer):

    user = UserArticleSerializers()
    comments = CommentsSerializers(many=True, read_only=True)

    class Meta:
        model = Article
        fields = '__all__'


#
# from .search_indexes import ArticlesIndex
# from drf_haystack.serializers import HaystackSerializer
# class ArticleIndexSerializer(HaystackSerializer):
#     class Meta:
#         index_classes = [ArticlesIndex]
#         fields = ['text', 'id', 'createtime', 'content', 'title']